package ga.coordination;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.security.acl.Group;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author user
 */
public class Map extends JFrame implements ChangeListener{
    
    Frame frm = new Frame("Coordinator");
    
    private JLabel jL1; //pilih map
    private JLabel jL2; //pilih metode search
    private JLabel jL3; //output file
    private JLabel jL4; //report
    private JLabel jL5; //mutation probability
    private JLabel jL6; //population size
    private JLabel jL7; //treshold
    private JLabel jL8; //crossover probability
    private JLabel jL9;
    private JTextField jTF1; //open path
    private JTextField jTF2; //save path
    private JTextField jTF3; //report news
    private JTextField jTF4;
    private JButton jB1; //browse open
    private JButton jB2; //startGA
    private JButton jB3; //stopGA
    private JButton jB4; //reset input
    private JButton jB5; //start simulation
    private JButton jB6; //browse save
    private JButton jB7; //restart simulation
    private JButton jB8; //back
    private JButton jB9; //Cancel
    private JPanel content1; //GUI input
    private JPanel content2; //GUI simulation
    private JPanel content3; //GUI report
    private JPanel content4; //GUI save
    private JPanel content5; //GA animation
    private JComboBox jCB1; //pilih search method
    private JSlider jS1;//mutation probability
    private JSlider jS2; //population size
    private JSlider jS3; //treshold
    private JSlider jS4; //crossover probability
    
    private boolean isGA = false;
    
    private int xSize = Toolkit.getDefaultToolkit().getScreenSize().width-400;
    private int ySize = Toolkit.getDefaultToolkit().getScreenSize().height-40;
    private int xAwal = 580;
    private int yAwal = 270;
    
    private Timer time;
    private int state = 0;
    
    public JPanel jp;
    /**
     * Creates new form Map
     */        
    public Map() throws InterruptedException { 
        File file = new File("C:\\Users\\Acer\\Desktop\\AI\\Tugas Besar 1 IB\\GA Coordinator\\testcase1.txt");
        this.world = new World(file);
        frm.setSize(xAwal,yAwal);
        
        /*
         * jS1 -> mutation probability
         */
        jS1 = new JSlider(JSlider.HORIZONTAL);
        jS1.setValue(50);
        jS1.setMaximum(100);
        jS1.setMinimum(0);
        jS1.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
        jS1.addChangeListener(this);
        /*
         * jS2 -> population size
         */
        jS2 = new JSlider();
        jS2.setValue(50);
        jS2.setMaximum(100);
        jS2.setMinimum(0);
        jS2.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
        jS2.addChangeListener(this);
        /*
         * jS3 -> Threshold
         */
        jS3 = new JSlider();
        jS3.setValue(50);
        jS3.setMaximum(100);
        jS3.setMinimum(0);
        jS3.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
        jS3.addChangeListener(this);
        /*
         * jS1 -> mutation probability
         */
        jS4 = new JSlider();
        jS4.setValue(50);
        jS4.setMaximum(100);
        jS4.setMinimum(0);
        jS4.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
        jS4.addChangeListener(this);
        /*
         * jL1 -> label insert map
         */
        jL1 = new JLabel("Insert map");
        jL1.setLabelFor(jTF1);
        /*
         * jL2 -> label search method
         */
        jL2 = new JLabel("Search Method : ");
        jL2.setLabelFor(jCB1);
        /*
         * jL3 -> label save hasil
         */
        jL3 = new JLabel("Output file");
        jL3.setLabelFor(jTF2);
        /*
         * jL4 -> label berita
         */
        jL4 = new JLabel("Report");
        jL4.setLabelFor(jTF3);/*
         * jL5 -> label mutation
         */
        jL5 = new JLabel("MutationProb : " + jS1.getValue());
        jL5.setLabelFor(jS1);/*
         * jL6 -> label population
         */
        jL6 = new JLabel("PopulationSz : " + jS2.getValue());
        jL6.setLabelFor(jS2);/*
         * jL7 -> label threshold
         */
        jL7 = new JLabel("Treshold     : " + jS3.getValue());
        jL7.setLabelFor(jS3);/*
         * jL8 -> label berita
         */
        jL8 = new JLabel("CrossoverProb: " + jS4.getValue());
        jL8.setLabelFor(jS4);
        /*
         * jL9
         */
        jL9 = new JLabel("Timer");
        jL9.setLabelFor(jTF4);
        /*
         * jCB1 -> pemilihan searching method
         */
        jCB1 = new JComboBox(new String[]{"BFS","UCS","A*"});
        jCB1.setSelectedIndex(0);
        jCB1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            }
        });
        /*
         * jTF1 -> textfield open file path
         */
        jTF1 = new JTextField();
        jTF1.setEditable(false);
        /*
         * jTF2 -> textfield save file path
         */
        jTF2 = new JTextField();
        jTF2.setEditable(false);
        /*
         * jTF3 -> textfield report
         */
        jTF3 = new JTextField();
        jTF3.setEditable(false);        
        /*
         * jTF4
         */
        jTF4 = new JTextField();
        /*
         * jB1 -> button browse
         */
        jB1 = new JButton("Browse");
        jB1.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jB1Action(e);
            }
        });
        jB1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        /*
         * jB2 -> button startGA
         */
        jB2 = new JButton("StartGA");
        jB2.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try {
                    jB2Action(e);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        jB2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        /*
         * jB3 -> button stop
         */
        jB3 = new JButton("Stop");
        jB3.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jB3Action(e);
            }
        });
        jB3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jB3.setEnabled(false);
        /*
         * jB4 -> button reset
         */
        jB4 = new JButton("Reset");
        jB4.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jB4Action(e);
            }
        });
        jB4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        /*
         * jB5 -> button start simulation
         */
        jB5 = new JButton("Start Simulation");
        jB5.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jB5Action(e);
            }
        });
        jB5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jB5.setEnabled(false);
        /*
         * jB6 -> button browse save
         */
        jB6 = new JButton("Browse");
        jB6.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jB6Action(e);
            }
        });
        jB6.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        /*
         * jB7 -> button reset
         */
        jB7 = new JButton("Restart");
        jB7.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jB7Action(e);
            }
        });
        jB7.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        /*
         * jB8 -> button back
         */
        jB8 = new JButton("Back");
        jB8.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jB8Action(e);
            }
        });
        /*
         * jB9 -> button back
         */
        jB9 = new JButton("<<< STOP ANIMATION");
        jB9.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                jB9Action(e);
            }
        });
        jB9.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        /*
         * content -> isi JPanel
         */
        content1 = new JPanel();        
        /*
         * content1 Layout
         */
        GroupLayout lO = new GroupLayout(content1);
        content1.setLayout(lO);
        lO.setAutoCreateGaps(true);
        lO.setAutoCreateContainerGaps(true);
        lO.setHorizontalGroup(
                lO.createSequentialGroup()
                .addGroup(lO.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jL1)
                    .addGroup(lO.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(jTF1)
                        .addGroup(lO.createSequentialGroup()
                            .addGroup(lO.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(jL2)
                                .addComponent(jB2)
                                .addComponent(jB3)
                                .addComponent(jB4))                                                        
                            .addGroup(lO.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(jCB1)
                                .addGroup(lO.createSequentialGroup()
                                    .addComponent(jL5)
                                    .addComponent(jS1))
                                .addGroup(lO.createSequentialGroup()
                                    .addComponent(jL6)
                                    .addComponent(jS2))
                                .addGroup(lO.createSequentialGroup()
                                    .addComponent(jL7)
                                    .addComponent(jS3))
                                .addGroup(lO.createSequentialGroup()
                                    .addComponent(jL8)
                                    .addComponent(jS4)))
                            .addComponent(jB5,GroupLayout.PREFERRED_SIZE,189,GroupLayout.PREFERRED_SIZE))))
                .addGroup(lO.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jB1)
                    .addComponent(jL9)
                .addComponent(jTF4))
                .addGap(500)
                );
        lO.setVerticalGroup(
                lO.createSequentialGroup()
                .addComponent(jL1)
                .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(jTF1)
                    .addComponent(jB1))
                .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(jL2)
                    .addComponent(jCB1))
                .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addGroup(lO.createSequentialGroup()
                        .addComponent(jB2)
                        .addComponent(jB3)
                        .addComponent(jB4))
                    .addGroup(lO.createSequentialGroup()
                        .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(jL5)
                            .addComponent(jS1))
                        .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(jL6)
                            .addComponent(jS2))
                        .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(jL7)
                            .addComponent(jS3))
                        .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(jL8)
                            .addComponent(jS4))
                        )
                    .addComponent(jB5,GroupLayout.PREFERRED_SIZE,90,GroupLayout.PREFERRED_SIZE)
                    .addGroup(lO.createSequentialGroup()
                        .addComponent(jL9)
                        .addComponent(jTF4))
                ));
        
        /*
         * Frame
         */
        frm.add(content1,BorderLayout.NORTH);
        frm.setVisible(true);
        
        /*
         * content -> isi JPanel
         */
        content2 = new JPanel();
        content3 = new JPanel();
        content4 = new JPanel();   
        content5 = new JPanel();
        
        /*
         * content2 layout
         */
        content2.removeAll();
        JPanel jp = new Map1(this); 
        jp.setSize(Toolkit.getDefaultToolkit().getScreenSize().width/20*15, Toolkit.getDefaultToolkit().getScreenSize().height/10*8);
        content2.add(jp);
        content2.setLayout(new GridLayout(0,1));
        
        /*
         * content3 layout
         */
        lO = new GroupLayout(content3);
        content3.setLayout(lO);
        lO.setAutoCreateGaps(true);
        lO.setAutoCreateContainerGaps(true);
        lO.setHorizontalGroup(
                lO.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(jL4)
                .addComponent(jTF3,GroupLayout.PREFERRED_SIZE,250,GroupLayout.PREFERRED_SIZE));
        lO.setVerticalGroup(
                lO.createSequentialGroup()
                .addComponent(jL4)
                .addComponent(jTF3,GroupLayout.PREFERRED_SIZE,550,GroupLayout.PREFERRED_SIZE));
        
        /*
         * content4 layout
         */
        lO = new GroupLayout(content4);
        content4.setLayout(lO);
        lO.setAutoCreateGaps(true);
        lO.setAutoCreateContainerGaps(true);
        lO.setHorizontalGroup(
                lO.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(jL3)
                .addGroup(lO.createSequentialGroup()
                    .addComponent(jTF2)
                    .addComponent(jB6)
                    .addComponent(jB7)
                    .addComponent(jB8))
                );
        lO.setVerticalGroup(
                lO.createSequentialGroup()
                .addComponent(jL3)
                .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(jTF2)
                    .addComponent(jB6)
                    .addComponent(jB7)
                    .addComponent(jB8)));
        
        lO = new GroupLayout(frm);
        frm.setLayout(lO);
        lO.setAutoCreateGaps(true);
        lO.setAutoCreateContainerGaps(false);
        lO.setHorizontalGroup(
                lO.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(content5,GroupLayout.PREFERRED_SIZE,500,GroupLayout.PREFERRED_SIZE)
                    .addComponent(content1)
                    .addGroup(lO.createSequentialGroup()
                        .addComponent(content2,GroupLayout.PREFERRED_SIZE,Toolkit.getDefaultToolkit().getScreenSize().width/20*9,GroupLayout.PREFERRED_SIZE)
                        .addComponent(content3,GroupLayout.PREFERRED_SIZE,Toolkit.getDefaultToolkit().getScreenSize().width/20*5,GroupLayout.PREFERRED_SIZE))
                    .addComponent(content4));                    
        lO.setVerticalGroup(
                lO.createSequentialGroup()
                    .addComponent(content5,GroupLayout.PREFERRED_SIZE,400,GroupLayout.PREFERRED_SIZE)
                    .addComponent(content1)
                    .addGroup(lO.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(content2,GroupLayout.PREFERRED_SIZE,Toolkit.getDefaultToolkit().getScreenSize().height/10*8,GroupLayout.PREFERRED_SIZE)
                        .addComponent(content3,GroupLayout.PREFERRED_SIZE,Toolkit.getDefaultToolkit().getScreenSize().height/10*8,GroupLayout.PREFERRED_SIZE))
                    .addComponent(content4));
        
        JPanel ga = new GAAnim();
        content5.add(ga);
        content5.add(jB9);
        content5.setLayout(new GridLayout(2,1));
        
        content2.setVisible(false);
        content3.setVisible(false);
        content4.setVisible(false);
        content5.setVisible(false);
        
        frm.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
        System.exit(0);
        }
        });        
    }
    
    private void jB1Action(ActionEvent e){
        //String filename = File.separator + "txt";
        //JFileChooser fc = new JFileChooser(new File (filename));
        JFileChooser fc = new JFileChooser();
        
        fc.showOpenDialog(null);
        jTF1.setText(fc.getSelectedFile().getPath());
    }
    
    World world;
    private void jB2Action(ActionEvent e) throws InterruptedException{
        String filePath = jTF1.getText();
        if(filePath.equals(""))
        {
            JLabel errInput = new JLabel("<HTML><FONT COLOR = Black>Pilih map terlebih dahulu.</FONT></HTML>");
            JOptionPane.showMessageDialog(null, errInput);
            jTF1.setText("");
            jCB1.setSelectedIndex(0);
        } else {
            jB3.setEnabled(true);
            jB2.setEnabled(false);
//            isGA = true;
//            content1.setVisible(false);
//            content5.setVisible(true);
//            frm.setSize(500, 400);
            
            
            File file = new File(jTF1.getText());
            world = new World(file);
            world.GenerateAllPairShortestPath(jCB1.getSelectedIndex());
            
            if (world.map !=null){
                GACoordinator gac = new GACoordinator(world.factory.hour, world.totalAllMachine,
                world.machineList, world.truckCapacity, world.pathList, world.factory, world.vendorList);
                
                //db
                jB5.setVisible(true); 
                jB5.setEnabled(true);
                
//                System.out.println(gac.solveSolution());
                
                // stop GA
                // output result
                // digenerate dulu,baru bisa di parse di GUI
                if (gac.pathTruckElitism!=null){
                    // ada solusi
                    
                    world.truck.generatePath(gac.compactIndividuElitism, gac.pathTruckElitism, world.pathList);
                    // DEBUG
                    world.truck.PrintPath();
                }
            }
            
            jp = new Map1(this); 
            
            
        }
    }
    
    private void jB3Action(ActionEvent e){
        jB3.setEnabled(false);
        jB2.setEnabled(true);
        jB5.setEnabled(true);
        isGA = true;
        content1.setVisible(false);
        content5.setVisible(true);
        frm.setSize(500, 400);
    }
    
    private void jB4Action(ActionEvent e){
        jTF1.setText("");   
        jCB1.setSelectedIndex(0);
        isGA = false;
        jB5.setEnabled(false);
        jB3.setEnabled(false);
    }
    
    private void jB5Action(ActionEvent e){
        if(!isGA)
        {
            JLabel errInput = new JLabel("<HTML><FONT COLOR = Black>Jalankan GA terlebih dahulu sebelum melakukan simulasi.</FONT></HTML>");
            JOptionPane.showMessageDialog(null, errInput);
        } else {
            isGA = false;
            content1.setVisible(false);
            content2.setVisible(true);
            content3.setVisible(true);
            content4.setVisible(true);  
            frm.setSize(xSize, ySize);
            state = 1;
        }
    }    
    
    private void jB6Action(ActionEvent e){
        //String filename = File.separator + "txt";
        //JFileChooser fc = new JFileChooser(new File (filename));
        JFileChooser fc = new JFileChooser();
        
        fc.showSaveDialog(null);
        jTF2.setText(fc.getSelectedFile().getPath());
        //Save to file
    }
    
    private void jB7Action(ActionEvent e){
        
    }
    
    private void jB8Action(ActionEvent e){
        content2.setVisible(false);
        content3.setVisible(false);
        content4.setVisible(false);
        content1.setVisible(true);
        frm.setSize(xAwal, yAwal);
        state = 0;
    }
    
    private void jB9Action(ActionEvent e){
        content5.setVisible(false);
        content1.setVisible(true);
        frm.setSize(xAwal, yAwal);
        state = 0;
    }
         
    public static void main (String[] args) throws InterruptedException{
        new Map();        
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if(jS1.getValueIsAdjusting()){
            jL5.setText("MutationProb : " + jS1.getValue());
        }
        if(jS2.getValueIsAdjusting()){
            jL6.setText("PopulationSz : " + jS2.getValue());
        }
        if(jS3.getValueIsAdjusting()){
            jL7.setText("Threshold    : " + jS3.getValue());
        }
        if(jS4.getValueIsAdjusting()){
            jL8.setText("CrossoverProb: " + jS4.getValue());
        }
    }
}